﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;
using System.Diagnostics;

namespace Gundam_2._0_Alpha.src
{
    public class Stage:IGameElement
    {
        private List<Button> buttonList = new List<Button>();
        private List<IAction> stageAction;
        private List<DestroyableObject> toDelete = new List<DestroyableObject>();
        private List<DestroyableObject> toAdd = new List<DestroyableObject>();
        private bool isMission;
        private List<DestroyableObject> charList;

        public DestroyableObject FindPlayer()
        {
            if (charList != null)
            {
                foreach (DestroyableObject player in charList)
                    if (player is Player)
                        return player;
            }
            return null;
        }

        public List<DestroyableObject> ToDelete
        {
            get { return toDelete; }
            set { toDelete = value; }
        }

        public List<DestroyableObject> ToAdd
        {
            get { return toAdd; }
            set { toAdd = value; }
        }

        public List<DestroyableObject> CharList
        {
            get { return charList; }
            set { charList = value; }
        }

        public List<IAction> StageAction
        {
            get { return stageAction; }
            set { stageAction = value; }
        }

        public List<Button> ButtonList
        {
            get { return buttonList; }
            set { buttonList = value; }
        }

        private Music BGM()
        {
            if (!isMission)
                return GlobalConst.menuMusic;
            else
                return GlobalConst.missionMusic;
        }

        private Bitmap background()
        {
            if (!isMission)
                return GlobalConst.menuBG;
            else
                return GlobalConst.missionBG;
        }

        public Stage(bool isMission)
        {
            this.isMission = isMission;
        }

        public void Update(GameController gc)
        {
            Debug.WriteLine("starting stage update");
            if (!Music.IsPlaying())
                BGM().Play();

            Debug.WriteLine("updating buttons");
            foreach (Button b in buttonList)
            {
                if (b.IsActive && Input.KeyTyped(b.K))
                        b.Update(gc);
                if (b.IsActive)
                {
                    b.ChangeSelectedButton();
                    Debug.WriteLine("stage update returning");
                    return;
                }
            }

            Debug.WriteLine("checking mission");
            if (isMission)
            {
                Debug.WriteLine("updating characnters - " + CharList.Count);
                foreach (DestroyableObject c in CharList)
                    c.Update(gc);

                Debug.WriteLine("removing objects - " + toDelete.Count);
                foreach (DestroyableObject c in toDelete)
                    charList.Remove(c);

                Debug.WriteLine("adding objects - " + toAdd.Count);
                foreach (DestroyableObject c in toAdd)
                    charList.Add(c);

                Debug.WriteLine("done... clearing");
                toDelete.Clear();
                toAdd.Clear();
            }
            Debug.WriteLine("stage actions");
            if (stageAction != null)
            {
                Debug.WriteLine("performing actions");
                foreach (IAction a in stageAction)
                    a.DoAction(gc);
                Debug.WriteLine("actions done");
            }

            Debug.WriteLine("ending stage update");
        }
        public void Draw()
        {
            background().Draw(0,0);
            foreach (Button b in buttonList)
                b.Draw();
            if (isMission)
            foreach (DestroyableObject c in CharList)
                c.Draw();
            if (stageAction != null)
                foreach (IAction a in stageAction)
                    if (a is IDrawAction)
                        (a as IDrawAction).Draw();

        }
    }
}
